import { createRouter, createWebHistory } from 'vue-router'
import cookies from 'vue-cookies'
import store from 'vuex'
import { defineAsyncComponent } from 'vue'

import NotFound from '@/views/Error.vue'


const routes = [
  {
    path: '/',
    name: 'login',
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/admin',
    name: 'admin',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/admin/frame.vue'),
    children: [
      // // 模板页路径
      // {
      //   path: '/admin/modal',
      //   name: 'admin_modal',
      //   meta: {
      //     isMenu: true,
      //     title: '模板页',
      //     componentUrl: defineAsyncComponent(() => {
      //       return import('@/views/admin/z_Modal/index.vue')
      //     })
      //   },
      //   component: () => import('@/views/admin/z_Modal/index.vue')
      // },
      // ===================================================== 企业管理
      {
        path: '/admin/enterprise',
        name: 'enterprise',
        meta: {
          isMenu: true,
          title: '企业管理',
          componentUrl: defineAsyncComponent(() => {
            return import('@/views/admin/enterprise/index.vue')
          })
        },
        component: () => import('@/views/admin/enterprise/index.vue')
      },
      // ===================================================== 密钥管理
      {
        path: '/admin/keys',
        name: 'keys',
        meta: {
          isMenu: true,
          title: '密钥管理',
          componentUrl: defineAsyncComponent(() => {
            return import('@/views/admin/keys/index.vue')
          })
        },
        component: () => import('@/views/admin/keys/index.vue')
      },
      // ===================================================== 企业用户
      {
        path: '/admin/user',
        name: 'user',
        meta: {
          isMenu: true,
          title: '企业用户',
          componentUrl: defineAsyncComponent(() => {
            return import('@/views/admin/user/index.vue')
          })
        },
        component: () => import('@/views/admin/user/index.vue')
      },
      // ===================================================== 代理商用户
      {
        path: '/admin/agentuser',
        name: 'agentuser',
        meta: {
          isMenu: true,
          title: '用户',
          componentUrl: defineAsyncComponent(() => {
            return import('@/views/admin/agentuser/index.vue')
          })
        },
        component: () => import('@/views/admin/agentuser/index.vue')
      },
      // ===================================================== 代理商角色
      {
        path: '/admin/agentrole',
        name: 'agentrole',
        meta: {
          isMenu: true,
          title: '角色',
          componentUrl: defineAsyncComponent(() => {
            return import('@/views/admin/agentRole/index.vue')
          })
        },
        component: () => import('@/views/admin/agentRole/index.vue')
      },
      // ===================================================== 企业角色
      {
        path: '/admin/role',
        name: 'role',
        meta: {
          isMenu: true,
          title: '企业角色',
          componentUrl: defineAsyncComponent(() => {
            return import('@/views/admin/role/index.vue')
          })
        },
        component: () => import('@/views/admin/role/index.vue')
      }

    ]
  },



  // 404 路由，放在最后
  { path: '/:pathMatch(.*)', component: NotFound }
  
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  let strpath = to.fullPath
  // console.log(to)
  if (to.meta.requireAuth) {
    // 是否登录 cookies.get('token')
    // if (true) {
      
    if (cookies.get('token') || true) {
      if (to.meta.isMenu) {
        store.useStore().commit('set_menuTabs', {
          name: to.name,
          path: to.path,
          title: to.meta.title?to.meta.title:'',
          componentUrl: to.meta.componentUrl?to.meta.componentUrl:'',
        })
        // console.log(store.useStore().state.menuTag)
      }
      next()
    } else {
      next({
        path: '/',
        query: { redirect: strpath }
      })
    }
  } else {
    // console.log('没有验证')
    next()
  }
})

export default router
